﻿/*
* List Form Manipulation Framework (LFMF) Javascript Library for SharePoint 2010 v1.1
* http://listFormManipulation.codeplex.com
*
* Author 2011-2012, Patrick Lamber
* http://www.lamber.info
*
* Date: June 30 2012
* 
* supported fieldTypes:
* SPFieldNumber; SPFieldCurrency; SPFieldText; SPFieldNote; SPFieldBoolean; SPFieldURL; SPFieldLookup; SPFieldLookupMulti; SPFieldChoice; SPFieldDateTime (Date only); SPTaxonomyFieldType, SPFieldUser, SPFieldIO
*/

var paramsGET = new Object();

if (window.location.search.length != 0) {
    var queryString = window.location.search.substring(1);
    if (queryString.indexOf("&") > -1) {
        var queryStringParts = queryString.split("&");
        for (var i = 0; i < queryStringParts.length; i++) {
            var queryParams = queryStringParts[i].split("=");
            paramsGET[queryParams[0]] = queryParams[1];
        }
    }
    else {
        var queryParams = queryString.split("=");
        paramsGET[queryParams[0]] = queryParams[1];
    }
};

function qppfSPFormField(displayName, type) {
    var firstMatch = true;

    this.InputControl;
    this.DescriptionControl;

    this.SPFieldDisplayName = displayName;
    this.SPFieldType = type;

    switch (type) {
        case "SPFieldNumber":
        case "SPFieldCurrency":
        case "SPFieldText":
            this.InputControl = jQuery('input[title="' + this.SPFieldDisplayName + '"][id$="TextField"]');
            break;
        case "SPFieldIO": 
            this.InputControl = jQuery('input[title="' + this.SPFieldDisplayName + '"][id$="onetidIOFile"]');
            break;
        case "SPFieldDateTime":
            this.InputControl = jQuery('input[title="' + this.SPFieldDisplayName + '"][id$="DateTimeField_DateTimeFieldDate"]');
            break;
        case "SPFieldNote":
            this.InputControl = jQuery('textarea[title="' + this.SPFieldDisplayName + '"][id$="TextField"]');

            // the standard way of finding the item does not work
            // it could be a enhanced richtext. Try to find it
            // Note: this works only with default forms
            if (this.InputControl.length == 0) {
                this.InputControl = jQuery('nobr:contains("' + this.SPFieldDisplayName + '")').closest('tr').find('input[id$="TextField_spSave"]');
            }
            break;
        case "SPFieldChoice":
            this.InputControl = jQuery('select[title="' + this.SPFieldDisplayName + '"][id$="DropDownChoice"]');

            // the standard way of finding the item does not work for Checkboxes and Radio buttons
            // Note: this works only with default forms
            if (this.InputControl.length == 0) {
                this.InputControl = jQuery('nobr:contains("' + this.SPFieldDisplayName + '")').closest('tr');

                firstMatch = false;
            }
            break;
        case "SPFieldBoolean":
            this.InputControl = jQuery('input[title="' + this.SPFieldDisplayName + '"][id$="BooleanField"]');
            break;
        case "SPFieldURL":
            this.InputControl = jQuery('input[title="' + this.SPFieldDisplayName + '"][id$="UrlFieldUrl"]');
            this.DescriptionControl = this.InputControl.siblings('[id$="UrlFieldDescription"]');
            break;
        case "SPFieldLookup":
            this.InputControl = jQuery('select[title="' + this.SPFieldDisplayName + '"][id$="Lookup"]');

            if (this.InputControl.length == 0) {
                this.InputControl = jQuery('input[title="' + this.SPFieldDisplayName + '"]');

                firstMatch = false;
            }
            break;
        case "SPFieldUser":
            this.InputControl = jQuery('nobr:contains("' + this.SPFieldDisplayName + '")').closest('tr').find("div[title='People Picker']");
            break;
        case "SPFieldLookupMulti":
            this.InputControl = jQuery('select[id$="SelectCandidate"][title^="' + this.SPFieldDisplayName + '"]');
            break;
        case "SPTaxonomyFieldType":
            this.InputControl = jQuery('div[class="ms-taxonomy-fieldeditor"][title^="' + this.SPFieldDisplayName + '"]');

            // this might happen when you selected the taxonomy field
            if (this.InputControl.length == 0) {
                this.InputControl = jQuery('div[class="ms-taxonomy-fieldeditor ms-taxonomy-writing"][title^="' + this.SPFieldDisplayName + '"]');
            }

            break;
        default:
            alert("Not able to find field with displayname: " + this.SPFieldDisplayName);
            break;
    };

    this.hideRow = function () {
        this.InputControl.closest('tr').hide();
    }

    this.setValue = function (value) {
        if (this.InputControl.length == 0) {
            return;
        }

        switch (this.SPFieldType) {
            case "SPFieldText":
            case "SPFieldNumber":
            case "SPFieldCurrency":
            case "SPFieldURL":
            case "SPFieldDateTime":
            case "SPFieldIO": 
            case "SPFieldNote":
                this.InputControl.val(value);
                break;
            case "SPFieldLookup":
                if (firstMatch) {
                    this.InputControl.val(value);
                }
                else {
                    var choices = this.InputControl.attr("choices");
                    var hiddenInput = jQuery("input[id='" + this.InputControl.attr("optHid") + "']");
                    hiddenInput.val(value);

                    var choiceArray = choices.split("|");
                    for (index = 1; index < choiceArray.length; index = index + 2) {
                        if (choiceArray[index] == value) {
                            this.InputControl.val(choiceArray[index - 1]);
                            break;
                        }
                    }
                }
                break;
            case "SPFieldChoice":
                if (firstMatch) {
                    this.InputControl.val(value);
                }
                else {
                    var radioButton = this.InputControl.find('[type="Radio"]').length > 0;

                    // if radio is true then it is a single value radiobutton control
                    if (radioButton) {
                        this.InputControl.find('span[title="' + value + '"]').find('input[type="radio"]').attr("checked", "checked");
                    }
                    // else it is a checkbox rendering
                    else {
                        var splitValue = value.split(';');

                        // uncheck all default checks
                        if (splitValue.length > 0) {
                            this.InputControl.find('input[type="checkbox"]').removeAttr("checked");
                        }

                        for (i = 0; i < splitValue.length; i++) {
                            this.InputControl.find('span[title="' + splitValue[i] + '"]').find('input[type="checkbox"]').attr("checked", "checked");
                        }
                    }
                }

                break;
            case "SPFieldLookupMulti":
                var tmpValue = ";" + value + ";";

                // remove default selections first
                this.InputControl.find("option").removeAttr("selected");

                this.InputControl.find("option").filter(function (index) {
                    var tmpText = ";" + jQuery(this).text() + ";";
                    return tmpValue.indexOf(tmpText) > -1;
                }).attr("selected", "selected");

                this.InputControl.dblclick();
                break;
            case "SPFieldUser":
                this.InputControl.text(value);
                break;
            case "SPFieldBoolean":
                this.InputControl.attr('checked', value);
                break;
            case "SPTaxonomyFieldType":
                var editableRegion = this.InputControl.find('div[id$="editableRegion"]');

                if (editableRegion.length > 0) {
                    editableRegion.html(value);
                    var taxonomyParent = editableRegion.parent().parent().parent();
                    if (taxonomyParent.length > 0) {
                        var taxonomyObject = new Microsoft.SharePoint.Taxonomy.ControlObject(taxonomyParent.get(0));
                        taxonomyObject.validateAll();
                    }
                }

                break;
            default:
                alert("Not able to find field with displayname: " + this.SPFieldDisplayName);
                break;
        }
    };

    this.getValue = function () {
        var result;
        switch (this.SPFieldType) {
            case "SPFieldText":
            case "SPFieldNumber":
            case "SPFieldCurrency":
            case "SPFieldURL":
            case "SPFieldDateTime":
            case "SPFieldIO": 
            case "SPFieldNote":
                result = this.InputControl.val();
                break;
            case "SPFieldLookup":
                if (firstMatch) {
                    result = this.InputControl.val();
                }
                else {
                    var hiddenInput = jQuery("input[id='" + this.InputControl.attr("optHid") + "']");
                    result = hiddenInput.val();
                }
                break;
            case "SPFieldChoice":
                if (firstMatch) {
                    result = this.InputControl.val();
                }
                else {
                    var radioButton = this.InputControl.find('[type="Radio"]').length > 0;

                    // if radio is true then it is a single value radiobutton control
                    if (radioButton) {
                        result = this.InputControl.find('span[title="' + value + '"]').find('input[type="radio"]').attr("checked");
                    }
                    // else it is a checkbox rendering
                    else {
                        alert("not yet implemented");
                    }
                }

                break;
            case "SPFieldLookupMulti":
            case "SPFieldBoolean":
            case "SPTaxonomyFieldType":
                alert("not yet implemented");
                break;
            default:
                alert("Not able to find field with displayname: " + this.SPFieldDisplayName);
                break;
        }

        return result;
    };

    this.setValueWithGETParam = function (queryParam) {
        this.setValue(paramsGET[queryParam]);
    }

    this.setDescription = function (value) {
        switch (this.SPFieldType) {
            case "SPFieldURL":
                this.DescriptionControl.val(value);
                break;
            default:
                break;
        }
    };
};